﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using FAP.DataAccess;
using System.Web;
using FAP.Cores.Task;
using FAP.DataAccess.BLL;

namespace FAP.Cores.Task
{
    public class UpTask : FapWork
    {
        public taikhoan m_tk;
        private string linkweb;

        public string LinkWeb
        {
            get { return linkweb; }
            set { linkweb = value; }
        }

        private string m_title;

        public string Title
        {
            get { return m_title; }
            set { m_title = value; }
        }

        private string m_content;

        public string Content
        {
            get { return m_content; }
            set { m_content = value; }
        }

        private string linkthread;

        public string LinkThread
        {
            get { return linkthread; }
            set { linkthread = value; }
        }

        private string idThread;

        public string IdThread
        {
            get { return idThread; }
            set { idThread = value; }
        }

        public UpTask(chitietnghiepvu nv, string content, string link, string lthread, string title)
            : base(nv)
        {
            this.TaskName = "UpTask";
            this.Content = content;
            this.linkweb = link;
            this.linkthread = lthread;
            this.Title = title;
        }
        public override taikhoan GetAccount()
        {
            return m_tk;
        }

        public override string GetContent()
        {
            return this.Content;
        }

        public override string GetTitle()
        {
            return this.Title;
        }

        public override string GetMaBaiViet()
        {
            string s = FindidThread();
            return s;
        }
        //public override string GetLinkThread()
        //{
        //    return this.LinkThread;
        //}

        //public override string GetHumanverify()
        //{
        //    string strTemp = HttpUtility.UrlDecode(this.HumanverifyFieldName);
        //    string human = FAP.Utility.HtmlHelper.GetValueByName(this.LastHTML, strTemp);
        //    return human;
        //}
        //private string stoken;
        public override string GetToken()
        {
            try
            {
                //if(this.LastHTML == null)
                //{
                //    return stoken;
                //}
                if (this.LastHTML.IndexOf("vBulletin&reg") != -1 || this.LastHTML.IndexOf("vBulletin&trade") != -1)
                {
                    int iTemp = this.LastHTML.IndexOf("SECURITYTOKEN");
                    string strTemp = this.LastHTML.Substring(iTemp);
                    int iStartToken = strTemp.IndexOf("\"");
                    int iEndToken = strTemp.IndexOf("\"", iStartToken + 1);
                    string t = strTemp.Substring(iStartToken + 1, iEndToken - iStartToken - 1);
                    //stoken = t;
                    return t;

                }

                string xftoken = TimKiem(this.LastHTML, "name=\"_xfToken\" value=\"");
                //stoken = xftoken;
                return xftoken;


                //khong lay dc cookie- khong dang nhap thanh cong
            }
            catch
            {
                FAP.Cores.Console.FapConsole.WriteLine("Up khong lay duoc token ", 4);
            }
            return null;
        }



        public override string GetBBUserid()
        {
            string cookie = requester.Cookie;
            cookie = cookie.Replace(';', ',');
            CookieContainer m_ccCookie = new CookieContainer();

            m_ccCookie.SetCookies(new Uri(linkweb), cookie);
            Uri u = new Uri(linkweb);
            string strLoggedinuser = null;
            try
            {
                strLoggedinuser = m_ccCookie.GetCookies(u)["bb_userid"].Value;
            }
            catch
            {
                //strLoggedinuser = m_ccCookie.GetCookies(u)["bbuserid"].Value;
            }
            int iUserId = -1;

            if (Int32.TryParse(strLoggedinuser, out iUserId))
            {
                return strLoggedinuser.ToString();
            }


            int iFirstIdx, iEndIdx, iLength;

            string strResult = this.LastHTML;
            string strTemp;
            if (strLoggedinuser == null)
            {
                try
                {
                    strTemp = "member.php?u=";
                    iFirstIdx = strResult.IndexOf(strTemp) + strTemp.Length;
                    //iFirstIdx = strResult.IndexOf('=', iFirstIdx+1)+1;
                    iEndIdx = strResult.IndexOf('\"', iFirstIdx);
                    iLength = iEndIdx - iFirstIdx;
                    strLoggedinuser = strResult.Substring(strResult.IndexOf(strTemp) + strTemp.Length, iLength);
                }
                catch { }
            }
            //moi them#######################

            if (!Int32.TryParse(strLoggedinuser, out iUserId))
            {
                strLoggedinuser = null;
                strTemp = "member/";
                iFirstIdx = strResult.IndexOf(strTemp) + strTemp.Length;
                //iFirstIdx = strResult.IndexOf('=', iFirstIdx+1)+1;
                iEndIdx = strResult.IndexOf('-', iFirstIdx);
                iLength = iEndIdx - iFirstIdx;
                strLoggedinuser = strResult.Substring(strResult.IndexOf(strTemp) + strTemp.Length, iLength);
            }
            //member.php?
            if (!Int32.TryParse(strLoggedinuser, out iUserId))
            {
                strLoggedinuser = null;
                strTemp = "member.php?";
                iFirstIdx = strResult.IndexOf(strTemp) + strTemp.Length;
                //iFirstIdx = strResult.IndexOf('=', iFirstIdx+1)+1;
                iEndIdx = strResult.IndexOf('-', iFirstIdx);
                iLength = iEndIdx - iFirstIdx;
                strLoggedinuser = strResult.Substring(strResult.IndexOf(strTemp) + strTemp.Length, iLength);
            }
            //#################################
            if (!Int32.TryParse(strLoggedinuser, out iUserId))
            {
                strLoggedinuser = null;
                strTemp = "members/";
                iFirstIdx = strResult.IndexOf(strTemp) + strTemp.Length;
                //iFirstIdx = strResult.IndexOf('=', iFirstIdx+1)+1;
                iEndIdx = strResult.IndexOf('/', iFirstIdx);
                iLength = iEndIdx - iFirstIdx;
                strLoggedinuser = strResult.Substring(strResult.IndexOf(strTemp) + strTemp.Length, iLength);


            }
            if (!Int32.TryParse(strLoggedinuser, out iUserId))//http://vozforums.com/member.php?u=249064
            {
                try
                {
                    strTemp = "member.php?u=";
                    iFirstIdx = strResult.IndexOf(strTemp) + strTemp.Length;
                    //iFirstIdx = strResult.IndexOf('=', iFirstIdx+1)+1;
                    iEndIdx = strResult.IndexOf('\"', iFirstIdx);
                    iLength = iEndIdx - iFirstIdx;
                    strLoggedinuser = strResult.Substring(strResult.IndexOf(strTemp) + strTemp.Length, iLength);
                }
                catch
                {
                }

            }
            //if (!Int32.TryParse(strLoggedinuser, out iUserId))
            //{
            //    strLoggedinuser = "";
            //    int iend = strResult.IndexOf("-" + web.m_strNick + "/") - 1;//http://www.vn-zoom.com/1342483-luuductran/
            //    for (int i = iend; i >= 0; i--)
            //    {
            //        char m = strResult[i];

            //        if (m == '"')
            //        {
            //            break;
            //        }
            //        else
            //        {
            //            strLoggedinuser += m.ToString();
            //        }
            //    }
            //    char[] arr = strLoggedinuser.ToCharArray();
            //    Array.Reverse(arr);
            //    strLoggedinuser = new string(arr);
            //}
            if (!Int32.TryParse(strLoggedinuser, out iUserId))
            {
                FAP.Cores.Console.FapConsole.WriteLine("Upper khong lay duoc BBuerid ", 4);
                return "";
            }

            return strLoggedinuser.ToString();
        }

        public string TimKiem(string html, string chuoitimkiem)
        {
            string strTemp = chuoitimkiem;
            int iFirstIdx = html.IndexOf(strTemp) + strTemp.Length;
            int iEndIdx = html.IndexOf("\"", html.IndexOf(strTemp) + strTemp.Length);
            int iLength = iEndIdx - iFirstIdx;
            string str = html.Substring(html.IndexOf(strTemp) + strTemp.Length, iLength);
            return str;
        }

        public string FindidThread()
        {

            int iStart = LinkThread.LastIndexOf("?t=") + 3;
            string strThread = LinkWeb + "newreply.php?do=postreply&";
            if (iStart < 3)
            {
                iStart = LinkThread.LastIndexOf("#post") + 5;
                //strThread += "p=";
            }
            else
            {
                strThread += "t=";
            }
            int iForum = -1;
            try//http://vozforums.com/showthread.php?t=3450653
            {
                iForum = Convert.ToInt32(LinkThread.Substring(iStart, LinkThread.Length - iStart));
                strThread += iForum;
            }
            catch
            {
                try//truong hop thread id la -xxxxx.html
                {
                    //truogn hop thread id nam dau tien http://www.vn-zoom.com/f77/co-cach-nao-go-code-tang-sub-ra-khong-2733428.html
                    iStart = LinkThread.LastIndexOf('-') + 1;
                    int iEnd = LinkThread.LastIndexOf('.');
                    string strNum = LinkThread.Substring(iStart, iEnd - iStart);
                    iForum = Convert.ToInt32(strNum);
                    strThread += "t=" + iForum;
                }
                catch
                {
                    try
                    {
                        //http://www.5giay.vn/linh-kien-dich-vu-dien-thoai/5845768-vo-iphone-5-case-iphone-5-op-lung-iphone-5-bao-da-iphone-5-bumper-iphone-5-gia-re.html
                        iStart = LinkThread.LastIndexOf('/') + 1;
                        int iEnd = LinkThread.IndexOf('-', iStart);
                        string strNum = LinkThread.Substring(iStart, iEnd - iStart);
                        iForum = Convert.ToInt32(strNum);
                        strThread += "t=" + iForum;
                    }
                    catch//http://localhost:9999/forum/showthread.php?1-dasd
                    {
                        iStart = LinkThread.LastIndexOf('/') + 1;
                        int iEnd = LinkThread.IndexOf('-', iStart);
                        string strNum = LinkThread.Substring(iStart, iEnd - iStart);
                        string[] arr = strNum.Split('?');
                        iForum = Convert.ToInt32(arr[1]);
                        strThread += "t=" + iForum;
                    }
                }
            }

            return iForum.ToString();

        }


    }

}